<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>3-1添加题目-单选题</title>
<link rel="stylesheet" type="text/css" href="../theme/1/css/common.css">
<link rel="stylesheet" type="text/css" href="../theme/1/css/style.css">
<link rel="stylesheet" type="text/css" href="../theme/1/css/EditingArea.css">
<link rel="stylesheet" type="text/css" href="../theme/1/css/table.css">
<link rel="stylesheet" type="text/css" href="../theme/1/css/icon.css">
<link rel="stylesheet" type="text/css" href="../theme/1/css/zy.css">
<script type="text/javascript" src="../jquery-2.1.4/jquery.min.js"></script>
<script>
	$(function(){
		//发送四条请求，将下拉菜单显示到屏幕上
		//获取类型
		function getAllSubjectType(handler){
			$.getJSON('http://127.0.0.1:3000/exam/manager/getAllSubjectType',function(data){
				var opts="";
				data.forEach(function(item,index){
					opts+='<option value="'+item.id+'">'+item.realName+'</option>'
				})
				var select='<span>类型</span>'
								+'<select name="subjectType">'
									+opts
								+'</select>';
				$('.opts').append(select);
				handler();
			})
		}
		// 获取难易程度
		function getAllSubjectLevel(handler){
			$.getJSON('http://127.0.0.1:3000/exam/manager/getAllSubjectLevel',function(data){
				var opts="";
				data.forEach(function(item,index){
					opts+='<option value="'+item.id+'">'+item.realName+'</option>'
				})
				var select='<span>难易程度</span>'
							+'<select name="subjectLevel">'
								+opts
							+'</select>';
				$('.opts').append(select);
				handler();
			})
		}
		// 获取方向
		function getAllDepartmentes(handler){
			$.getJSON('http://127.0.0.1:3000/exam/manager/getAllDepartmentes',function(data){
				var opts="";
				data.forEach(function(item,index){
						opts+='<option value="'+item.id+'">'+item.name+'</option>'
				})
				var select='<span>方向</span>'
										+'<select name="departmente">'
											+opts
										+'</select>';
				$('.opts').append(select);
				handler();
			})
		}
		// 获取知识点
		function getAllTopics(handler){
			$.getJSON('http://127.0.0.1:3000/exam/manager/getAllTopics',function(data){
				var opts="";
				data.forEach(function(item,index){
						opts+='<option value="'+item.id+'">'+item.title+'</option>'
				})
				var select='<span>知识点</span>'
								+'<select name="topics">'
									+opts
								+'</select>';
				$('.opts').append(select);
				handler();
			})
		}

	//执行上述4个方法
		getAllSubjectType(function(){
			$('select[name="subjectType"]').on('change',function(){
				//当点击复选框的时候，将下方的单选框改为复选框
				if($(this).val()==2){
					$('input[type="radio"]').attr("type","checkbox");
					$('.Answercontent').css("display","block");
				//当点击单选框的时候，将下方的复选框改为单选框
				}else if($(this).val()==1){
					$('input[type="checkbox"]').attr("type","radio");
					$('.Answercontent').css("display","block");
				// 当点击简答的时候，将下方的复选框单选框移除
				}else {
					$('.Answercontent').css("display","none");
				}

			})
		});
		getAllSubjectLevel(function(){});
		getAllDepartmentes(function(){});
		getAllTopics(function(){});
		var choiceCorrect="";
		//当点击保存并继续按钮时，通过ajax发送数据
		$('.btn_left .btnL').on('click',function(){
			choiceCorrect=[0,0,0,0];
			if($('.Answerpart input').attr("type")=="radio"){
				var radio_val = $('input[name="question"]:checked').val();
				choiceCorrect[radio_val]=1;

			}
			else {
				$('input[name="question"]:checked').each(function(index,item){
					// console.log(item.value);
					choiceCorrect[item.value]=1;
				});
			}

			var subjectDatas={
				subjectTypeId:$('select[name="subjectType"]').val(),
				subjectLevelId:$('select[name="subjectLevel"]').val(),
				departmentId:$('select[name="departmente"]').val(),
				topicId:$('select[name="topics"]').val(),
				stem:$('textarea[name="question_bank"]').val(),
				answer:$('textarea[name="question_answer"]').val()
			}


			$.ajax({
				url : "http://127.0.0.1:3000/exam/manager/saveSubject",
				method : "post",
				data :JSON.stringify(subjectDatas) ,
				contentType : "application/json",
				success : function(data){
					var answerDatas={
						choiceContent: [
											$('.Answercontent textarea').eq(0).val(),
											$('.Answercontent textarea').eq(1).val(),
											$('.Answercontent textarea').eq(2).val(),
											$('.Answercontent textarea').eq(3).val()
										],
						choiceCorrect:choiceCorrect,
						subject_id:data.insertId

					}
					//当添加的题目为简答题的时候，不需要添加答案
					if($('select[name=subjectType]').val()!=3)
					{
						$.ajax({
							url : "http://127.0.0.1:3000/exam/manager/saveAnswer",
							method : "post",
							data :JSON.stringify(answerDatas) ,
							contentType : "application/json",
							success : function(data){
							}
						})
					}
					location.reload();

				}
			});

		})








	})
</script>
</head>

<body>
<div class="main">
	<div id="top">
    	<div class="top">
        	<div class="tk_logo"></div>
            <div class="t_sign">
            	<span>你好，管理员</span><a href="#" class="t_exit">退出</a>
            </div>
            <div class="t_icon">
            	<ul>
                	<li><a href="#" class="t1"><span>桌面</span></a></li>
                    <li><a href="#" class="t2"><span>换肤</span></a></li>
                    <li><a href="#" class="t3"><span>关于</span></a></li>
                </ul>
            </div>
        </div>
    </div>
    <div id="cont">
    	<div class="centent">
        	<div class="left c_view">
            	<ul class="baseUI">
                    <li><a href="#"><em class="base_basicset"></em><span>题库</span></a>
                        <ul>
                            <li class="current"><a href="#">全部题目</a></li>
                            <li><a href="#">题目管理</a></li>
                        </ul>
                    </li>
                    <li><a href="#"><em class="base_userset"></em><span>试卷管理</span></a>
                        <ul>
                            <li><a href="#">试卷列表</a></li>
                            <li><a href="#">手工组卷</a></li>
                        </ul>
                    </li>
                    <li><a href="#"><em class="base_teachergroup"></em><span>考试管理</span></a>
                        <ul>
                            <li><a href="#">考试列表</a></li>
                            <li><a href="#">考试安排</a></li>
                        </ul>
                    </li>

                </ul>
            </div>
            <div class="right">
            	<div class="editingarea">
                    <div class="c_flex"><span class="c_flexible"></span></div>
                    <div class="Topictitle">添加题目</div>
                	<div class="c_editview">
												<!--这个div中存放多个题目的选项-->
												<div class="opts">
														<!-- <span>类型</span>
														<select name="subjectType">
															<option value="">单选题</option>
															<option value="">多选题</option>
															<option value="">简答题</option>
														</select> -->
												</div>
                        <div class="Problem">
                            <div class="Attributetit">题目题干</div>
                            <div class="Problemcontent">
								<!-- 增加一个文本域 -->
								<textarea name="question_bank" rows="4" cols="80"></textarea>
                            </div>
                        </div>
                        <div class="Answeroptions">
                            <div class="Attributetit">答案选项<em>(通过勾选每个选项下面的框难吃时间点咳嗽打开)</em></div>
                            <div class="c_condition"><span class="icon_add"><em class="icon_r" style="float: left">添加选项</em></span></div>
                            <div class="Answercontent">
                                  <div class="Answerpart">
                                     <div class="Answerpart_left">
                                         <p>A</p><span><input type="radio" name="question" value="0"/></span>
                                     </div>
                                     <div class="Answerpart_right">
                                     	<textarea name="name" rows="5" cols="45"></textarea>
                                     </div>
                                     <div class="clear"></div>
                                  </div>
                                <div class="Answerpart">
                                    <div class="Answerpart_left">
                                        <p>B</p><span><input type="radio" name="question" value="1"/></span>
                                    </div>
                                    <div class="Answerpart_right">
										<textarea name="name" rows="5" cols="45"></textarea>
                                    </div>
                                    <div class="clear"></div>
                                </div>
                                <div class="Answerpart">
                                    <div class="Answerpart_left">
                                        <p>C</p><span><input type="radio" name="question" value="2"/></span>
                                    </div>
                                    <div class="Answerpart_right">
										<textarea name="name" rows="5" cols="45"></textarea>
                                    </div>
                                    <div class="clear"></div>
                                </div>
                                <div class="Answerpart">
                                    <div class="Answerpart_left">
                                        <p>D</p><span><input type="radio" name="question"  value="3"/></span>
                                    </div>
                                    <div class="Answerpart_right">
										<textarea name="name" rows="5" cols="45"></textarea>
                                    </div>
                                    <div class="clear"></div>
                                </div>
                                  <div class="clear"></div>
                            </div>
                        </div>
                        <div class="Problem">
                            <div class="Attributetit">答案解析</div>
                            <div class="Problemcontent">
								<!-- 增加一个文本域 -->
                            	<textarea name="question_answer" rows="4" cols="80"></textarea>
                            </div>
                        </div>

                        <div class="btn_left">
                            <span class="btnL"><em class="btnR">保存并继续</em></span><span class="btnL"><em class="btnR"><a href="1-1题库-全部题库.html">关闭</a></em></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="clear"></div>
        </div>
    </div>
    <div id="foot"><a href="#">上海师悦信息科技有限公司</a></div>
</div>
</body>
</html>
